package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDunHuangShiKuEventAction;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_HELP_RECORD_OPERATION_FROM_CROSS, desc = "敦煌石窟领取队友互助奖励/协助攻击 跨服返回区服 协议")
public class DunHuangShikuHelpRecordOperationFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationToGameServerMsg respMsg = DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationToGameServerMsg.parseFrom(packet.getBytes());
        DunHuangShiKuProto.DunHuangShiKuHelpRecordOperationRespMsg.Builder respClientMsg = respMsg.getRespClient().toBuilder();
        ret = respClientMsg.getRet();
        if(ret == 0){
            // 加道具
            CurrencyModule currencyModule = player.getModule(CurrencyModule.class);
            Property reward = PropertyHelper.parseStringToProperty(respClientMsg.getReward());
            StringBuilder rewardSb = new StringBuilder();
            if(reward != null){
                rewardSb.append(respClientMsg.getReward());
                currencyModule.addCurrency(reward, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuMuralReward);
            }
            // 加积分
            if(respClientMsg.getScore() > 0){
                if(rewardSb.length() > 0){
                    rewardSb.append(";");
                }
                Property scoreProperty = new Property(DunHuangShiKuMgr.DUNHUANGSHIKU_SCORE_ITEM_ID, BigInteger.valueOf(respClientMsg.getScore()));
                rewardSb.append(PropertyHelper.parsePropertyToString(scoreProperty));
            }
            if(respClientMsg.getAllScore() > 0){
                player.notifyListener(eGamePlayerEventType.DunHuangShiKuScoreCrossRank.getValue(), respClientMsg.getAllScore());
            }
            // 加日志
            if(respClientMsg.hasLog()){
                long damage = respClientMsg.getLog().getBeforeAttackBlood() - respClientMsg.getLog().getAfterAttackBlood();
                DunHuangShiKuProto.DunHuangShiKuMuralActionLogEntity log = respClientMsg.getLog();
                AutoLogMgr.add(new LogDunHuangShiKuEventAction(DunHuangShiKuMgr.activityInfo.getActivityId(), player.getUserId(), log.getTeamId(), log.getRegionId(), log.getEventType(), 4, log.getTargetId(), log.getStatus(), log.getBeforeAttackBlood(), log.getAfterAttackBlood(), damage, rewardSb.toString()));
            }
        }
        player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_HELP_RECORD_OPERATION, respClientMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
